use crate::score::{RoundResult, TeamState};
#[derive(Debug)]
pub struct Table {
    // 0: my group 1: op group
    pub team_states: [TeamState; 4],
    pub frist_banker:i8,
    //every round result
    pub rounds: Vec<RoundResult>,
    pub is_game_finished: bool,
    pub winner_team_id: i16,
    pub sec_team_id: i16,
    pub third_team_id: i16,
    pub last_team_id: i16,
    // score delta between 1st and third teams
    pub score_fs: i16,
    // score delta between 1st and last teams
    pub score_fl: i16,
    // 1: 2v2  2: solo
    pub mode: i16,
    pub final_score: i16,
    pub limit_bag: i16,
    pub score_trick: i16,
    pub score_bag: i16,
    pub score_nil: i16,
    pub score_blind_nil: i16,
    pub score_bag_penalty: i16,
    pub score_lose: i16,
    pub joker: i16,
    pub max_round:i16
}

impl Table {
    pub fn new() -> Self {
        Self {
            team_states: [
                TeamState::new(0),
                TeamState::new(1),
                TeamState::new(2),
                TeamState::new(3),
            ],
            rounds: vec![],
            is_game_finished: false,
            winner_team_id: -1,
            mode: 1,
            final_score: 500,
            limit_bag: 10,
            score_trick: 10,
            score_bag: 1,
            score_nil: 100,
            score_blind_nil: 200,
            score_bag_penalty: 100,
            score_lose: -200,
            joker: 0,
            sec_team_id: -1,
            third_team_id: -1,
            last_team_id: -1,
            score_fs: 0,
            score_fl: 0,
            frist_banker:-1,
	        max_round: -1,
        }
    }

    pub fn reset(&mut self) {
        self.team_states = [
            TeamState::new(0),
            TeamState::new(1),
            TeamState::new(2),
            TeamState::new(3),
        ];
        self.rounds.clear();
        self.is_game_finished = false;
        self.winner_team_id = -1;
        self.sec_team_id = -1;
        self.third_team_id = -1;
        self.last_team_id = -1;
        self.frist_banker = -1;
    }

    pub fn check_game_finish(&mut self) -> bool {
        // reach max round
        let teams = &self.team_states;
        for team_state in teams.iter() {
            if team_state.score >= self.final_score || team_state.score <= self.score_lose {
                self.is_game_finished = true;
                break;
            }
        }
        //max round limit finish
        if self.max_round > -1 && self.rounds.len() as i16 >= self.max_round {
            self.is_game_finished = true;
        }
        //平局按照lose 算
        if self.is_game_finished {
            if self.mode == 1 {
                let team_state0 = &self.team_states[0];
                let team_state1 = &self.team_states[1];
                if team_state0.score > team_state1.score {
                    self.winner_team_id = team_state0.id as i16;
                } else if team_state1.score > team_state0.score {
                    self.winner_team_id = team_state1.id as i16;
                } else {
                    self.winner_team_id = -1;
                }
            } else if self.mode == 2 {
                // Sort teams in descending order based on scores
                // Create a clone of team_states for sorting
                let mut sorted_teams: [TeamState; 4] = self.team_states.clone();

                // Sort the clone in descending order based on scores
                sorted_teams.sort_by(|a, b| b.score.cmp(&a.score));

                // Record team IDs from the sorted clone
                self.winner_team_id = sorted_teams[0].id as i16;
                self.sec_team_id = sorted_teams[1].id as i16;
                self.third_team_id = sorted_teams[2].id as i16;
                self.last_team_id = sorted_teams[3].id as i16;

                // Record score differences using the original order
                self.score_fs = self.team_states[0].score - self.team_states[2].score;
                self.score_fl = self.team_states[0].score - self.team_states[3].score;
            }
        }
        self.is_game_finished
    }
}
